package com.huai.demo_test1.controller;

import com.huai.demo_test1.pojo.Student;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author 淮晓欢
 * @date 2021/12/28
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/test")
public class TestController {
    @GetMapping("/echo")
    public void test(){
        log.info("这是测试");
    }

    /**
     * 请求路径：http://localhost:8080/test/echo/1/淮晓欢
    */
    @GetMapping("/echo/{id}/{name}")
    public String testPathVariable(@PathVariable Integer id,@PathVariable String name){
        return "id:"+id+"，name:"+name;
    }

    /**
     * 请求路径：http://localhost:8080/test/echo/requestParams 请求方式 get
    */
    @GetMapping("/echo/requestParams")
    public String testRequestParam(@RequestParam(value ="name",required = false) String name,
                                         @RequestParam("age")Integer age){
        return "接收姓名请求参数：" +name+",接收年龄请求参数：" +age;
    }

    /**
     * 请求路径：http://localhost:8080/test/echo/requestBody 请求方式 post
     * 请求体：
     * {
     *     "id":1,
     *     "name":"huai",
     *     "age":22,
     *     "gender":"女"
     * }
    */
    @PostMapping("/echo/requestBody")
    public Student testRequestBody(@RequestBody Student student){
        log.info("接收姓名请求参数：" +student.getName()+",接收年龄请求参数：" +student.getAge()+",接收性别请求参数："+student.getGender());
        //测试提交
        return student;
    }


    /**
     * 请求路径：http://localhost:8080/test/echo/requestParams 请求方式 get
     */
    @GetMapping("/echo/request")
    public String testRequestParamReq(@RequestParam(value ="name") String name,
                                   @RequestParam("age")Integer age){
        return "接收姓名请求参数：" +name+",接收年龄请求参数：" +age;
    }

}
